﻿using System;
using System.Collections.Generic;
using System.Formats.Tar;
using System.IO;
using System.Linq;
using System.Net;
using AppData;

public class BooksSaver
{
    public static string[] AllBookFileExt = { ".pdf", "epub" };

    //public static List<Book> GetAllBooks(BookGenre genre)
    //{
    //    string bookDir = GetBookDir(ServerConfig.booksRootDir, genre);

    //    if (Directory.Exists(bookDir))
    //    {
    //        string[] files = Directory.GetFiles(bookDir)
    //            .ToArray();

    //        List<Book> books = new List<Book>();
    //        for(int i = 0; i < files.Length; i++)
    //        {
    //            string bookId = Path.GetFileNameWithoutExtension(files[i]);

    //            var newBook = new Book();
    //            newBook.contentData = LoadBook(bookId, genre);
    //        }
    //        return books; 
    //    }
    //    return null;
    //}


   
    public static int SaveBook(string genre, long bookId, string fileExt, byte[] contentData)
    {
        return SaveBook(ServerConfig.booksRootDir, genre, bookId, fileExt, contentData);
    }
    public static byte[] LoadBook(string bookId, string genre)
    {
        return LoadBook(ServerConfig.booksRootDir, bookId, genre,"");
    }
  
    public static void DeleteBook(string booksRootDir, string bookId, string genre)
    {
        DeleteBook(GetBookDir(booksRootDir,genre), bookId);
    }
    public static void DeleteBook(string bookDir, string bookId)
    {
        foreach (var ext in AllBookFileExt)
        {
            string texFilePath = GetTexFilePath(bookDir, bookId, ext);
            if (File.Exists(texFilePath))
                File.Delete(texFilePath);
        }
    }
    public static int SaveBook(string booksRootDir, string genre, long bookId, string fileExt,byte[] contentData)
    {
        try
        {
            if (contentData == null || contentData.Length <= 0)
            {
                GameDebug.LogError("SaveBook error");
                return -1;
            }
            string bookDir = booksRootDir + "/" + genre.ToString();
            if (!Directory.Exists(bookDir))
            {
                GameDebug.Log($"create Book dir:{bookDir}");
                Directory.CreateDirectory(bookDir);
            }
            //string texString = Encoding.UTF8.GetString(tex);
            string texFilePath = GetTexFilePath(bookDir, bookId.ToString(), fileExt);
            //string infoFilePath = GetInfoFilePath(bookDir, book.id.ToString());

            //string info = GetInfo(book.info);

            File.WriteAllBytes(texFilePath, contentData);
            //File.WriteAllText(infoFilePath, info);
            return 0;
        }
        catch (Exception ex)
        {
            GameDebug.LogException(ex);
            return -1;
        }
    }

    public static string GetBookDir(string booksRootDir, string genre) => booksRootDir + "/" + genre.ToString();

    static byte[] TryGetBookFile(string bookDir,string bookId, string fileExt)
    {
        if (!string.IsNullOrEmpty(fileExt))
        {
            string texFile = GetTexFilePath(bookDir, bookId, fileExt);
            if (File.Exists(texFile))
                return File.ReadAllBytes(texFile);
        }
        foreach (var ext in AllBookFileExt)
        {
            string bookFile = GetTexFilePath(bookDir, bookId, ext);
            if (File.Exists(bookFile))
                return File.ReadAllBytes(bookFile);
        }
        return null;
    }

    public static byte[] LoadBook(string booksRootDir, string bookId, string genre,string ext)
    {
        string bookDir = GetBookDir(booksRootDir , genre);

        //LoadInfo(bookDir, bookId, genre.ToString(), out BookInfo bookInfo);

        var bookFile = TryGetBookFile(bookDir, bookId, ext);
        return bookFile;
        //{
        //    Book book = new Book();
        //    long.TryParse(bookId, out long bookIdLong);
        //    book.id = bookIdLong;
        //    book.info = bookInfo;
        //    book.contentData = bookFile;
        //    return book;
        //}
        //return null;
    }

    //public static string GetInfo(BookInfo bookInfo)
    //{
    //    return Newtonsoft.Json.JsonConvert.SerializeObject(bookInfo);
    //}

    //public static void LoadInfo(string booksRootDir, string bookId, string genre, out BookInfo book)
    //{
    //    book = null;

    //    string infoFilePath = GetInfoFilePath(booksRootDir, bookId);
    //    if (File.Exists(infoFilePath))
    //    {
    //        string info = File.ReadAllText(infoFilePath);

    //        book = Newtonsoft.Json.JsonConvert.DeserializeObject<BookInfo>(info);
    //    }
    //}

    public static string GetTexFilePath(string bookDir, string bookId, string ext)
    {
        string fileName = bookId + ext;
        string filePath = bookDir + "/" + fileName;
        return filePath;
    }
    //public static string GetInfoFilePath(string bookDir, string bookId)
    //{
    //    string fileName = bookId + ".txt";
    //    string filePath = bookDir + "/" + fileName; 
    //    return filePath;
    //}
}

